package com.bwie.user.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.bwie.user.entity.GoodsEntity;
import com.bwie.user.service.GoodsService;
import com.bwie.user.vo.ActivityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.user.entity.ActivityEntity;
import com.bwie.user.service.ActivityService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 这是一张秒杀活动表
 *
 * @author ÕÔË«Ò¶
 * @email 1535715783@qq.com
 * @date 2022-02-22 10:58:45
 */
@RestController
@RequestMapping("user/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;




    /**
     * 防刷 ---------------------
     */
    @RequestMapping("/fangshua/{goodsId}/{userId}")
    public R fangshua(@PathVariable("goodsId") Long goodsId,
                      @PathVariable("userId")Long userId, HttpServletRequest request){
        //生成一个随机数

        if (userId==null){

            return R.error("客户端请求错误");
        }

        //限制访问次数 -------接口安全：接口防刷（接口限流）（防止接口恶意攻击导致服务宕机）
        String uri = request.getRequestURI();
        String key = uri + "_" + userId;
        System.out.println(key);

        Integer count = (Integer) redisTemplate.opsForValue().get(key);
        System.out.println(count);

        if (count==null){//存入次数并且获取过期时间
            redisTemplate.opsForValue().set(key,1,30,TimeUnit.SECONDS);


        }else if (count<5){//可以访问
            //次数+1
            redisTemplate.opsForValue().set(key,count+1);


        }else {//超过5次

            return R.error("访问次数过多");
        }


        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        redisTemplate.opsForValue().set(goodsId+userId,uuid,30, TimeUnit.SECONDS);


        return R.ok().setData(uuid);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = activityService.queryPage(params);

        return R.ok().put("page", page);
    }


    //秒杀列活动表页面
    @RequestMapping("/voList")
    public R voList(@RequestParam(value = "goods_name",defaultValue = "")String goodsName){

        List<ActivityVo> vos=activityService.voList(goodsName);

//        PageUtils page=activityService.voList2(params);

        return R.ok().setData(vos);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{actId}")
    public R info(@PathVariable("actId") Long actId){
		ActivityEntity activity = activityService.getById(actId);
        //根据查到的订单查寻商品信息
        GoodsEntity goodsEntity = goodsService.getById(activity.getGoodsId());

        return R.ok().put("activity", activity).put("goodsEntity",goodsEntity);
    }


    /**
     * 根据id查
     */
    @RequestMapping("/infoTo/{actId}")
    public R infoTo(@PathVariable("actId") Long actId){
        ActivityEntity activity = activityService.getById(actId);


        return R.ok().setData(activity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ActivityEntity activity){
		activityService.save(activity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ActivityEntity activity){
		activityService.updateById(activity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] actIds){
		activityService.removeByIds(Arrays.asList(actIds));

        return R.ok();
    }

}
